# test list insert method by self method (author: D_w)

# my_list = [3, 1, 2, 5, 4, 9, 7, 2]
# 使用python底层方法实现
# 尾部插入元素
# my_list.append(6)
# 中间插入元素
# my_list.insert(5, 11)
# print(my_list)


# d_w 自己实现插入
class MyArray:
    def __init__(self, capacity):
        self.array = [None] * capacity
        self.size = 0

    def insert_v2(self, index, element):
        if index < 0 or index > self.size:
            raise Exception("超出数据实际元素范围")
        if self.size >= len(self.array):
            self.resize()
        # only after the size>len(self.array) works
        for i in range(self.size-1, index-1, -1):
            self.array[i+1] = self.array[i]
        self.array[index] = element
        self.size += 1

    def resize(self):
        array_new = [None] * len(self.array) * 2
        for i in range(self.size):
            array_new[i] = self.array[i]
        self.array = array_new

    def output(self):
        array_new = [None] * len(self.array) * 2
        for i in range(self.size):
            print(self.array[i])

    def remove(self, index):
        if index < 0 or index >= self.size:
            raise Exception("超出数组实际元素范围")
        for i in range(index, self.size):
            self.array[i] = self.array[i + 1]
        self.size -= 1


a = MyArray(8)
print(a.size)
print(a.array)
a.insert_v2(0, 2)
a.insert_v2(1, 3)
a.insert_v2(2, 4)
a.insert_v2(3, 5)
a.insert_v2(4, 6)
a.insert_v2(5, 7)
a.insert_v2(6, 8)
a.insert_v2(7, 9)
a.insert_v2(1, 0)
print(len(a.array), a.size)
a.insert_v2(1, 0)
print(len(a.array), a.size)
# a.insert_v2(8, 9)
# a.insert_v2(9, 9)
# print(a.array)
# print(a.size, len(a.array))
# a.remove(4)
# print(a.array)
# print(a.output())
